{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7c90d0d9",
   "metadata": {},
   "source": [
    "# Adversarial Attack against Object Recognition"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2083003f",
   "metadata": {},
   "source": [
    "### Imports and Initial Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3d3f5b36",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-27T10:14:17.507972Z",
     "start_time": "2022-12-27T10:14:17.256998Z"
    }
   },
   "source": [
    "%matplotlib widget"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "66176dd5",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-27T10:14:18.508737Z",
     "start_time": "2022-12-27T10:14:17.916739Z"
    }
   },
   "source": [
    "import cv2\n",
    "import torch\n",
    "import torchvision\n",
    "import numpy as np\n",
    "from matplotlib import pyplot as plt\n",
    "\n",
    "import torch.nn as nn\n",
    "from torch.autograd import Variable\n",
    "from torchvision.io import read_image\n",
    "from torchvision.models import resnet50, ResNet50_Weights\n",
    "\n",
    "np.random.seed(42)\n",
    "torch.manual_seed(42)"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6548c8f5",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-27T10:14:18.537120Z",
     "start_time": "2022-12-27T10:14:18.510021Z"
    },
    "code_folding": [
     0
    ]
   },
   "source": [
    "IMAGENET_ID2LABEL = {0: 'tench, Tinca tinca',\n",
    " 1: 'goldfish, Carassius auratus',\n",
    " 2: 'great white shark, white shark, man-eater, man-eating shark, Carcharodon carcharias',\n",
    " 3: 'tiger shark, Galeocerdo cuvieri',\n",
    " 4: 'hammerhead, hammerhead shark',\n",
    " 5: 'electric ray, crampfish, numbfish, torpedo',\n",
    " 6: 'stingray',\n",
    " 7: 'cock',\n",
    " 8: 'hen',\n",
    " 9: 'ostrich, Struthio camelus',\n",
    " 10: 'brambling, Fringilla montifringilla',\n",
    " 11: 'goldfinch, Carduelis carduelis',\n",
    " 12: 'house finch, linnet, Carpodacus mexicanus',\n",
    " 13: 'junco, snowbird',\n",
    " 14: 'indigo bunting, indigo finch, indigo bird, Passerina cyanea',\n",
    " 15: 'robin, American robin, Turdus migratorius',\n",
    " 16: 'bulbul',\n",
    " 17: 'jay',\n",
    " 18: 'magpie',\n",
    " 19: 'chickadee',\n",
    " 20: 'water ouzel, dipper',\n",
    " 21: 'kite',\n",
    " 22: 'bald eagle, American eagle, Haliaeetus leucocephalus',\n",
    " 23: 'vulture',\n",
    " 24: 'great grey owl, great gray owl, Strix nebulosa',\n",
    " 25: 'European fire salamander, Salamandra salamandra',\n",
    " 26: 'common newt, Triturus vulgaris',\n",
    " 27: 'eft',\n",
    " 28: 'spotted salamander, Ambystoma maculatum',\n",
    " 29: 'axolotl, mud puppy, Ambystoma mexicanum',\n",
    " 30: 'bullfrog, Rana catesbeiana',\n",
    " 31: 'tree frog, tree-frog',\n",
    " 32: 'tailed frog, bell toad, ribbed toad, tailed toad, Ascaphus trui',\n",
    " 33: 'loggerhead, loggerhead turtle, Caretta caretta',\n",
    " 34: 'leatherback turtle, leatherback, leathery turtle, Dermochelys coriacea',\n",
    " 35: 'mud turtle',\n",
    " 36: 'terrapin',\n",
    " 37: 'box turtle, box tortoise',\n",
    " 38: 'banded gecko',\n",
    " 39: 'common iguana, iguana, Iguana iguana',\n",
    " 40: 'American chameleon, anole, Anolis carolinensis',\n",
    " 41: 'whiptail, whiptail lizard',\n",
    " 42: 'agama',\n",
    " 43: 'frilled lizard, Chlamydosaurus kingi',\n",
    " 44: 'alligator lizard',\n",
    " 45: 'Gila monster, Heloderma suspectum',\n",
    " 46: 'green lizard, Lacerta viridis',\n",
    " 47: 'African chameleon, Chamaeleo chamaeleon',\n",
    " 48: 'Komodo dragon, Komodo lizard, dragon lizard, giant lizard, Varanus komodoensis',\n",
    " 49: 'African crocodile, Nile crocodile, Crocodylus niloticus',\n",
    " 50: 'American alligator, Alligator mississipiensis',\n",
    " 51: 'triceratops',\n",
    " 52: 'thunder snake, worm snake, Carphophis amoenus',\n",
    " 53: 'ringneck snake, ring-necked snake, ring snake',\n",
    " 54: 'hognose snake, puff adder, sand viper',\n",
    " 55: 'green snake, grass snake',\n",
    " 56: 'king snake, kingsnake',\n",
    " 57: 'garter snake, grass snake',\n",
    " 58: 'water snake',\n",
    " 59: 'vine snake',\n",
    " 60: 'night snake, Hypsiglena torquata',\n",
    " 61: 'boa constrictor, Constrictor constrictor',\n",
    " 62: 'rock python, rock snake, Python sebae',\n",
    " 63: 'Indian cobra, Naja naja',\n",
    " 64: 'green mamba',\n",
    " 65: 'sea snake',\n",
    " 66: 'horned viper, cerastes, sand viper, horned asp, Cerastes cornutus',\n",
    " 67: 'diamondback, diamondback rattlesnake, Crotalus adamanteus',\n",
    " 68: 'sidewinder, horned rattlesnake, Crotalus cerastes',\n",
    " 69: 'trilobite',\n",
    " 70: 'harvestman, daddy longlegs, Phalangium opilio',\n",
    " 71: 'scorpion',\n",
    " 72: 'black and gold garden spider, Argiope aurantia',\n",
    " 73: 'barn spider, Araneus cavaticus',\n",
    " 74: 'garden spider, Aranea diademata',\n",
    " 75: 'black widow, Latrodectus mactans',\n",
    " 76: 'tarantula',\n",
    " 77: 'wolf spider, hunting spider',\n",
    " 78: 'tick',\n",
    " 79: 'centipede',\n",
    " 80: 'black grouse',\n",
    " 81: 'ptarmigan',\n",
    " 82: 'ruffed grouse, partridge, Bonasa umbellus',\n",
    " 83: 'prairie chicken, prairie grouse, prairie fowl',\n",
    " 84: 'peacock',\n",
    " 85: 'quail',\n",
    " 86: 'partridge',\n",
    " 87: 'African grey, African gray, Psittacus erithacus',\n",
    " 88: 'macaw',\n",
    " 89: 'sulphur-crested cockatoo, Kakatoe galerita, Cacatua galerita',\n",
    " 90: 'lorikeet',\n",
    " 91: 'coucal',\n",
    " 92: 'bee eater',\n",
    " 93: 'hornbill',\n",
    " 94: 'hummingbird',\n",
    " 95: 'jacamar',\n",
    " 96: 'toucan',\n",
    " 97: 'drake',\n",
    " 98: 'red-breasted merganser, Mergus serrator',\n",
    " 99: 'goose',\n",
    " 100: 'black swan, Cygnus atratus',\n",
    " 101: 'tusker',\n",
    " 102: 'echidna, spiny anteater, anteater',\n",
    " 103: 'platypus, duckbill, duckbilled platypus, duck-billed platypus, Ornithorhynchus anatinus',\n",
    " 104: 'wallaby, brush kangaroo',\n",
    " 105: 'koala, koala bear, kangaroo bear, native bear, Phascolarctos cinereus',\n",
    " 106: 'wombat',\n",
    " 107: 'jellyfish',\n",
    " 108: 'sea anemone, anemone',\n",
    " 109: 'brain coral',\n",
    " 110: 'flatworm, platyhelminth',\n",
    " 111: 'nematode, nematode worm, roundworm',\n",
    " 112: 'conch',\n",
    " 113: 'snail',\n",
    " 114: 'slug',\n",
    " 115: 'sea slug, nudibranch',\n",
    " 116: 'chiton, coat-of-mail shell, sea cradle, polyplacophore',\n",
    " 117: 'chambered nautilus, pearly nautilus, nautilus',\n",
    " 118: 'Dungeness crab, Cancer magister',\n",
    " 119: 'rock crab, Cancer irroratus',\n",
    " 120: 'fiddler crab',\n",
    " 121: 'king crab, Alaska crab, Alaskan king crab, Alaska king crab, Paralithodes camtschatica',\n",
    " 122: 'American lobster, Northern lobster, Maine lobster, Homarus americanus',\n",
    " 123: 'spiny lobster, langouste, rock lobster, crawfish, crayfish, sea crawfish',\n",
    " 124: 'crayfish, crawfish, crawdad, crawdaddy',\n",
    " 125: 'hermit crab',\n",
    " 126: 'isopod',\n",
    " 127: 'white stork, Ciconia ciconia',\n",
    " 128: 'black stork, Ciconia nigra',\n",
    " 129: 'spoonbill',\n",
    " 130: 'flamingo',\n",
    " 131: 'little blue heron, Egretta caerulea',\n",
    " 132: 'American egret, great white heron, Egretta albus',\n",
    " 133: 'bittern',\n",
    " 134: 'crane',\n",
    " 135: 'limpkin, Aramus pictus',\n",
    " 136: 'European gallinule, Porphyrio porphyrio',\n",
    " 137: 'American coot, marsh hen, mud hen, water hen, Fulica americana',\n",
    " 138: 'bustard',\n",
    " 139: 'ruddy turnstone, Arenaria interpres',\n",
    " 140: 'red-backed sandpiper, dunlin, Erolia alpina',\n",
    " 141: 'redshank, Tringa totanus',\n",
    " 142: 'dowitcher',\n",
    " 143: 'oystercatcher, oyster catcher',\n",
    " 144: 'pelican',\n",
    " 145: 'king penguin, Aptenodytes patagonica',\n",
    " 146: 'albatross, mollymawk',\n",
    " 147: 'grey whale, gray whale, devilfish, Eschrichtius gibbosus, Eschrichtius robustus',\n",
    " 148: 'killer whale, killer, orca, grampus, sea wolf, Orcinus orca',\n",
    " 149: 'dugong, Dugong dugon',\n",
    " 150: 'sea lion',\n",
    " 151: 'Chihuahua',\n",
    " 152: 'Japanese spaniel',\n",
    " 153: 'Maltese dog, Maltese terrier, Maltese',\n",
    " 154: 'Pekinese, Pekingese, Peke',\n",
    " 155: 'Shih-Tzu',\n",
    " 156: 'Blenheim spaniel',\n",
    " 157: 'papillon',\n",
    " 158: 'toy terrier',\n",
    " 159: 'Rhodesian ridgeback',\n",
    " 160: 'Afghan hound, Afghan',\n",
    " 161: 'basset, basset hound',\n",
    " 162: 'beagle',\n",
    " 163: 'bloodhound, sleuthhound',\n",
    " 164: 'bluetick',\n",
    " 165: 'black-and-tan coonhound',\n",
    " 166: 'Walker hound, Walker foxhound',\n",
    " 167: 'English foxhound',\n",
    " 168: 'redbone',\n",
    " 169: 'borzoi, Russian wolfhound',\n",
    " 170: 'Irish wolfhound',\n",
    " 171: 'Italian greyhound',\n",
    " 172: 'whippet',\n",
    " 173: 'Ibizan hound, Ibizan Podenco',\n",
    " 174: 'Norwegian elkhound, elkhound',\n",
    " 175: 'otterhound, otter hound',\n",
    " 176: 'Saluki, gazelle hound',\n",
    " 177: 'Scottish deerhound, deerhound',\n",
    " 178: 'Weimaraner',\n",
    " 179: 'Staffordshire bullterrier, Staffordshire bull terrier',\n",
    " 180: 'American Staffordshire terrier, Staffordshire terrier, American pit bull terrier, pit bull terrier',\n",
    " 181: 'Bedlington terrier',\n",
    " 182: 'Border terrier',\n",
    " 183: 'Kerry blue terrier',\n",
    " 184: 'Irish terrier',\n",
    " 185: 'Norfolk terrier',\n",
    " 186: 'Norwich terrier',\n",
    " 187: 'Yorkshire terrier',\n",
    " 188: 'wire-haired fox terrier',\n",
    " 189: 'Lakeland terrier',\n",
    " 190: 'Sealyham terrier, Sealyham',\n",
    " 191: 'Airedale, Airedale terrier',\n",
    " 192: 'cairn, cairn terrier',\n",
    " 193: 'Australian terrier',\n",
    " 194: 'Dandie Dinmont, Dandie Dinmont terrier',\n",
    " 195: 'Boston bull, Boston terrier',\n",
    " 196: 'miniature schnauzer',\n",
    " 197: 'giant schnauzer',\n",
    " 198: 'standard schnauzer',\n",
    " 199: 'Scotch terrier, Scottish terrier, Scottie',\n",
    " 200: 'Tibetan terrier, chrysanthemum dog',\n",
    " 201: 'silky terrier, Sydney silky',\n",
    " 202: 'soft-coated wheaten terrier',\n",
    " 203: 'West Highland white terrier',\n",
    " 204: 'Lhasa, Lhasa apso',\n",
    " 205: 'flat-coated retriever',\n",
    " 206: 'curly-coated retriever',\n",
    " 207: 'golden retriever',\n",
    " 208: 'Labrador retriever',\n",
    " 209: 'Chesapeake Bay retriever',\n",
    " 210: 'German short-haired pointer',\n",
    " 211: 'vizsla, Hungarian pointer',\n",
    " 212: 'English setter',\n",
    " 213: 'Irish setter, red setter',\n",
    " 214: 'Gordon setter',\n",
    " 215: 'Brittany spaniel',\n",
    " 216: 'clumber, clumber spaniel',\n",
    " 217: 'English springer, English springer spaniel',\n",
    " 218: 'Welsh springer spaniel',\n",
    " 219: 'cocker spaniel, English cocker spaniel, cocker',\n",
    " 220: 'Sussex spaniel',\n",
    " 221: 'Irish water spaniel',\n",
    " 222: 'kuvasz',\n",
    " 223: 'schipperke',\n",
    " 224: 'groenendael',\n",
    " 225: 'malinois',\n",
    " 226: 'briard',\n",
    " 227: 'kelpie',\n",
    " 228: 'komondor',\n",
    " 229: 'Old English sheepdog, bobtail',\n",
    " 230: 'Shetland sheepdog, Shetland sheep dog, Shetland',\n",
    " 231: 'collie',\n",
    " 232: 'Border collie',\n",
    " 233: 'Bouvier des Flandres, Bouviers des Flandres',\n",
    " 234: 'Rottweiler',\n",
    " 235: 'German shepherd, German shepherd dog, German police dog, alsatian',\n",
    " 236: 'Doberman, Doberman pinscher',\n",
    " 237: 'miniature pinscher',\n",
    " 238: 'Greater Swiss Mountain dog',\n",
    " 239: 'Bernese mountain dog',\n",
    " 240: 'Appenzeller',\n",
    " 241: 'EntleBucher',\n",
    " 242: 'boxer',\n",
    " 243: 'bull mastiff',\n",
    " 244: 'Tibetan mastiff',\n",
    " 245: 'French bulldog',\n",
    " 246: 'Great Dane',\n",
    " 247: 'Saint Bernard, St Bernard',\n",
    " 248: 'Eskimo dog, husky',\n",
    " 249: 'malamute, malemute, Alaskan malamute',\n",
    " 250: 'Siberian husky',\n",
    " 251: 'dalmatian, coach dog, carriage dog',\n",
    " 252: 'affenpinscher, monkey pinscher, monkey dog',\n",
    " 253: 'basenji',\n",
    " 254: 'pug, pug-dog',\n",
    " 255: 'Leonberg',\n",
    " 256: 'Newfoundland, Newfoundland dog',\n",
    " 257: 'Great Pyrenees',\n",
    " 258: 'Samoyed, Samoyede',\n",
    " 259: 'Pomeranian',\n",
    " 260: 'chow, chow chow',\n",
    " 261: 'keeshond',\n",
    " 262: 'Brabancon griffon',\n",
    " 263: 'Pembroke, Pembroke Welsh corgi',\n",
    " 264: 'Cardigan, Cardigan Welsh corgi',\n",
    " 265: 'toy poodle',\n",
    " 266: 'miniature poodle',\n",
    " 267: 'standard poodle',\n",
    " 268: 'Mexican hairless',\n",
    " 269: 'timber wolf, grey wolf, gray wolf, Canis lupus',\n",
    " 270: 'white wolf, Arctic wolf, Canis lupus tundrarum',\n",
    " 271: 'red wolf, maned wolf, Canis rufus, Canis niger',\n",
    " 272: 'coyote, prairie wolf, brush wolf, Canis latrans',\n",
    " 273: 'dingo, warrigal, warragal, Canis dingo',\n",
    " 274: 'dhole, Cuon alpinus',\n",
    " 275: 'African hunting dog, hyena dog, Cape hunting dog, Lycaon pictus',\n",
    " 276: 'hyena, hyaena',\n",
    " 277: 'red fox, Vulpes vulpes',\n",
    " 278: 'kit fox, Vulpes macrotis',\n",
    " 279: 'Arctic fox, white fox, Alopex lagopus',\n",
    " 280: 'grey fox, gray fox, Urocyon cinereoargenteus',\n",
    " 281: 'tabby, tabby cat',\n",
    " 282: 'tiger cat',\n",
    " 283: 'Persian cat',\n",
    " 284: 'Siamese cat, Siamese',\n",
    " 285: 'Egyptian cat',\n",
    " 286: 'cougar, puma, catamount, mountain lion, painter, panther, Felis concolor',\n",
    " 287: 'lynx, catamount',\n",
    " 288: 'leopard, Panthera pardus',\n",
    " 289: 'snow leopard, ounce, Panthera uncia',\n",
    " 290: 'jaguar, panther, Panthera onca, Felis onca',\n",
    " 291: 'lion, king of beasts, Panthera leo',\n",
    " 292: 'tiger, Panthera tigris',\n",
    " 293: 'cheetah, chetah, Acinonyx jubatus',\n",
    " 294: 'brown bear, bruin, Ursus arctos',\n",
    " 295: 'American black bear, black bear, Ursus americanus, Euarctos americanus',\n",
    " 296: 'ice bear, polar bear, Ursus Maritimus, Thalarctos maritimus',\n",
    " 297: 'sloth bear, Melursus ursinus, Ursus ursinus',\n",
    " 298: 'mongoose',\n",
    " 299: 'meerkat, mierkat',\n",
    " 300: 'tiger beetle',\n",
    " 301: 'ladybug, ladybeetle, lady beetle, ladybird, ladybird beetle',\n",
    " 302: 'ground beetle, carabid beetle',\n",
    " 303: 'long-horned beetle, longicorn, longicorn beetle',\n",
    " 304: 'leaf beetle, chrysomelid',\n",
    " 305: 'dung beetle',\n",
    " 306: 'rhinoceros beetle',\n",
    " 307: 'weevil',\n",
    " 308: 'fly',\n",
    " 309: 'bee',\n",
    " 310: 'ant, emmet, pismire',\n",
    " 311: 'grasshopper, hopper',\n",
    " 312: 'cricket',\n",
    " 313: 'walking stick, walkingstick, stick insect',\n",
    " 314: 'cockroach, roach',\n",
    " 315: 'mantis, mantid',\n",
    " 316: 'cicada, cicala',\n",
    " 317: 'leafhopper',\n",
    " 318: 'lacewing, lacewing fly',\n",
    " 319: \"dragonfly, darning needle, devil's darning needle, sewing needle, snake feeder, snake doctor, mosquito hawk, skeeter hawk\",\n",
    " 320: 'damselfly',\n",
    " 321: 'admiral',\n",
    " 322: 'ringlet, ringlet butterfly',\n",
    " 323: 'monarch, monarch butterfly, milkweed butterfly, Danaus plexippus',\n",
    " 324: 'cabbage butterfly',\n",
    " 325: 'sulphur butterfly, sulfur butterfly',\n",
    " 326: 'lycaenid, lycaenid butterfly',\n",
    " 327: 'starfish, sea star',\n",
    " 328: 'sea urchin',\n",
    " 329: 'sea cucumber, holothurian',\n",
    " 330: 'wood rabbit, cottontail, cottontail rabbit',\n",
    " 331: 'hare',\n",
    " 332: 'Angora, Angora rabbit',\n",
    " 333: 'hamster',\n",
    " 334: 'porcupine, hedgehog',\n",
    " 335: 'fox squirrel, eastern fox squirrel, Sciurus niger',\n",
    " 336: 'marmot',\n",
    " 337: 'beaver',\n",
    " 338: 'guinea pig, Cavia cobaya',\n",
    " 339: 'sorrel',\n",
    " 340: 'zebra',\n",
    " 341: 'hog, pig, grunter, squealer, Sus scrofa',\n",
    " 342: 'wild boar, boar, Sus scrofa',\n",
    " 343: 'warthog',\n",
    " 344: 'hippopotamus, hippo, river horse, Hippopotamus amphibius',\n",
    " 345: 'ox',\n",
    " 346: 'water buffalo, water ox, Asiatic buffalo, Bubalus bubalis',\n",
    " 347: 'bison',\n",
    " 348: 'ram, tup',\n",
    " 349: 'bighorn, bighorn sheep, cimarron, Rocky Mountain bighorn, Rocky Mountain sheep, Ovis canadensis',\n",
    " 350: 'ibex, Capra ibex',\n",
    " 351: 'hartebeest',\n",
    " 352: 'impala, Aepyceros melampus',\n",
    " 353: 'gazelle',\n",
    " 354: 'Arabian camel, dromedary, Camelus dromedarius',\n",
    " 355: 'llama',\n",
    " 356: 'weasel',\n",
    " 357: 'mink',\n",
    " 358: 'polecat, fitch, foulmart, foumart, Mustela putorius',\n",
    " 359: 'black-footed ferret, ferret, Mustela nigripes',\n",
    " 360: 'otter',\n",
    " 361: 'skunk, polecat, wood pussy',\n",
    " 362: 'badger',\n",
    " 363: 'armadillo',\n",
    " 364: 'three-toed sloth, ai, Bradypus tridactylus',\n",
    " 365: 'orangutan, orang, orangutang, Pongo pygmaeus',\n",
    " 366: 'gorilla, Gorilla gorilla',\n",
    " 367: 'chimpanzee, chimp, Pan troglodytes',\n",
    " 368: 'gibbon, Hylobates lar',\n",
    " 369: 'siamang, Hylobates syndactylus, Symphalangus syndactylus',\n",
    " 370: 'guenon, guenon monkey',\n",
    " 371: 'patas, hussar monkey, Erythrocebus patas',\n",
    " 372: 'baboon',\n",
    " 373: 'macaque',\n",
    " 374: 'langur',\n",
    " 375: 'colobus, colobus monkey',\n",
    " 376: 'proboscis monkey, Nasalis larvatus',\n",
    " 377: 'marmoset',\n",
    " 378: 'capuchin, ringtail, Cebus capucinus',\n",
    " 379: 'howler monkey, howler',\n",
    " 380: 'titi, titi monkey',\n",
    " 381: 'spider monkey, Ateles geoffroyi',\n",
    " 382: 'squirrel monkey, Saimiri sciureus',\n",
    " 383: 'Madagascar cat, ring-tailed lemur, Lemur catta',\n",
    " 384: 'indri, indris, Indri indri, Indri brevicaudatus',\n",
    " 385: 'Indian elephant, Elephas maximus',\n",
    " 386: 'African elephant, Loxodonta africana',\n",
    " 387: 'lesser panda, red panda, panda, bear cat, cat bear, Ailurus fulgens',\n",
    " 388: 'giant panda, panda, panda bear, coon bear, Ailuropoda melanoleuca',\n",
    " 389: 'barracouta, snoek',\n",
    " 390: 'eel',\n",
    " 391: 'coho, cohoe, coho salmon, blue jack, silver salmon, Oncorhynchus kisutch',\n",
    " 392: 'rock beauty, Holocanthus tricolor',\n",
    " 393: 'anemone fish',\n",
    " 394: 'sturgeon',\n",
    " 395: 'gar, garfish, garpike, billfish, Lepisosteus osseus',\n",
    " 396: 'lionfish',\n",
    " 397: 'puffer, pufferfish, blowfish, globefish',\n",
    " 398: 'abacus',\n",
    " 399: 'abaya',\n",
    " 400: \"academic gown, academic robe, judge's robe\",\n",
    " 401: 'accordion, piano accordion, squeeze box',\n",
    " 402: 'acoustic guitar',\n",
    " 403: 'aircraft carrier, carrier, flattop, attack aircraft carrier',\n",
    " 404: 'airliner',\n",
    " 405: 'airship, dirigible',\n",
    " 406: 'altar',\n",
    " 407: 'ambulance',\n",
    " 408: 'amphibian, amphibious vehicle',\n",
    " 409: 'analog clock',\n",
    " 410: 'apiary, bee house',\n",
    " 411: 'apron',\n",
    " 412: 'ashcan, trash can, garbage can, wastebin, ash bin, ash-bin, ashbin, dustbin, trash barrel, trash bin',\n",
    " 413: 'assault rifle, assault gun',\n",
    " 414: 'backpack, back pack, knapsack, packsack, rucksack, haversack',\n",
    " 415: 'bakery, bakeshop, bakehouse',\n",
    " 416: 'balance beam, beam',\n",
    " 417: 'balloon',\n",
    " 418: 'ballpoint, ballpoint pen, ballpen, Biro',\n",
    " 419: 'Band Aid',\n",
    " 420: 'banjo',\n",
    " 421: 'bannister, banister, balustrade, balusters, handrail',\n",
    " 422: 'barbell',\n",
    " 423: 'barber chair',\n",
    " 424: 'barbershop',\n",
    " 425: 'barn',\n",
    " 426: 'barometer',\n",
    " 427: 'barrel, cask',\n",
    " 428: 'barrow, garden cart, lawn cart, wheelbarrow',\n",
    " 429: 'baseball',\n",
    " 430: 'basketball',\n",
    " 431: 'bassinet',\n",
    " 432: 'bassoon',\n",
    " 433: 'bathing cap, swimming cap',\n",
    " 434: 'bath towel',\n",
    " 435: 'bathtub, bathing tub, bath, tub',\n",
    " 436: 'beach wagon, station wagon, wagon, estate car, beach waggon, station waggon, waggon',\n",
    " 437: 'beacon, lighthouse, beacon light, pharos',\n",
    " 438: 'beaker',\n",
    " 439: 'bearskin, busby, shako',\n",
    " 440: 'beer bottle',\n",
    " 441: 'beer glass',\n",
    " 442: 'bell cote, bell cot',\n",
    " 443: 'bib',\n",
    " 444: 'bicycle-built-for-two, tandem bicycle, tandem',\n",
    " 445: 'bikini, two-piece',\n",
    " 446: 'binder, ring-binder',\n",
    " 447: 'binoculars, field glasses, opera glasses',\n",
    " 448: 'birdhouse',\n",
    " 449: 'boathouse',\n",
    " 450: 'bobsled, bobsleigh, bob',\n",
    " 451: 'bolo tie, bolo, bola tie, bola',\n",
    " 452: 'bonnet, poke bonnet',\n",
    " 453: 'bookcase',\n",
    " 454: 'bookshop, bookstore, bookstall',\n",
    " 455: 'bottlecap',\n",
    " 456: 'bow',\n",
    " 457: 'bow tie, bow-tie, bowtie',\n",
    " 458: 'brass, memorial tablet, plaque',\n",
    " 459: 'brassiere, bra, bandeau',\n",
    " 460: 'breakwater, groin, groyne, mole, bulwark, seawall, jetty',\n",
    " 461: 'breastplate, aegis, egis',\n",
    " 462: 'broom',\n",
    " 463: 'bucket, pail',\n",
    " 464: 'buckle',\n",
    " 465: 'bulletproof vest',\n",
    " 466: 'bullet train, bullet',\n",
    " 467: 'butcher shop, meat market',\n",
    " 468: 'cab, hack, taxi, taxicab',\n",
    " 469: 'caldron, cauldron',\n",
    " 470: 'candle, taper, wax light',\n",
    " 471: 'cannon',\n",
    " 472: 'canoe',\n",
    " 473: 'can opener, tin opener',\n",
    " 474: 'cardigan',\n",
    " 475: 'car mirror',\n",
    " 476: 'carousel, carrousel, merry-go-round, roundabout, whirligig',\n",
    " 477: \"carpenter's kit, tool kit\",\n",
    " 478: 'carton',\n",
    " 479: 'car wheel',\n",
    " 480: 'cash machine, cash dispenser, automated teller machine, automatic teller machine, automated teller, automatic teller, ATM',\n",
    " 481: 'cassette',\n",
    " 482: 'cassette player',\n",
    " 483: 'castle',\n",
    " 484: 'catamaran',\n",
    " 485: 'CD player',\n",
    " 486: 'cello, violoncello',\n",
    " 487: 'cellular telephone, cellular phone, cellphone, cell, mobile phone',\n",
    " 488: 'chain',\n",
    " 489: 'chainlink fence',\n",
    " 490: 'chain mail, ring mail, mail, chain armor, chain armour, ring armor, ring armour',\n",
    " 491: 'chain saw, chainsaw',\n",
    " 492: 'chest',\n",
    " 493: 'chiffonier, commode',\n",
    " 494: 'chime, bell, gong',\n",
    " 495: 'china cabinet, china closet',\n",
    " 496: 'Christmas stocking',\n",
    " 497: 'church, church building',\n",
    " 498: 'cinema, movie theater, movie theatre, movie house, picture palace',\n",
    " 499: 'cleaver, meat cleaver, chopper',\n",
    " 500: 'cliff dwelling',\n",
    " 501: 'cloak',\n",
    " 502: 'clog, geta, patten, sabot',\n",
    " 503: 'cocktail shaker',\n",
    " 504: 'coffee mug',\n",
    " 505: 'coffeepot',\n",
    " 506: 'coil, spiral, volute, whorl, helix',\n",
    " 507: 'combination lock',\n",
    " 508: 'computer keyboard, keypad',\n",
    " 509: 'confectionery, confectionery, candy store',\n",
    " 510: 'container ship, containership, container vessel',\n",
    " 511: 'convertible',\n",
    " 512: 'corkscrew, bottle screw',\n",
    " 513: 'cornet, horn, trumpet, trump',\n",
    " 514: 'cowboy boot',\n",
    " 515: 'cowboy hat, ten-gallon hat',\n",
    " 516: 'cradle',\n",
    " 517: 'crane',\n",
    " 518: 'crash helmet',\n",
    " 519: 'crate',\n",
    " 520: 'crib, cot',\n",
    " 521: 'Crock Pot',\n",
    " 522: 'croquet ball',\n",
    " 523: 'crutch',\n",
    " 524: 'cuirass',\n",
    " 525: 'dam, dike, dyke',\n",
    " 526: 'desk',\n",
    " 527: 'desktop computer',\n",
    " 528: 'dial telephone, dial phone',\n",
    " 529: 'diaper, nappy, napkin',\n",
    " 530: 'digital clock',\n",
    " 531: 'digital watch',\n",
    " 532: 'dining table, board',\n",
    " 533: 'dishrag, dishcloth',\n",
    " 534: 'dishwasher, dish washer, dishwashing machine',\n",
    " 535: 'disk brake, disc brake',\n",
    " 536: 'dock, dockage, docking facility',\n",
    " 537: 'dogsled, dog sled, dog sleigh',\n",
    " 538: 'dome',\n",
    " 539: 'doormat, welcome mat',\n",
    " 540: 'drilling platform, offshore rig',\n",
    " 541: 'drum, membranophone, tympan',\n",
    " 542: 'drumstick',\n",
    " 543: 'dumbbell',\n",
    " 544: 'Dutch oven',\n",
    " 545: 'electric fan, blower',\n",
    " 546: 'electric guitar',\n",
    " 547: 'electric locomotive',\n",
    " 548: 'entertainment center',\n",
    " 549: 'envelope',\n",
    " 550: 'espresso maker',\n",
    " 551: 'face powder',\n",
    " 552: 'feather boa, boa',\n",
    " 553: 'file, file cabinet, filing cabinet',\n",
    " 554: 'fireboat',\n",
    " 555: 'fire engine, fire truck',\n",
    " 556: 'fire screen, fireguard',\n",
    " 557: 'flagpole, flagstaff',\n",
    " 558: 'flute, transverse flute',\n",
    " 559: 'folding chair',\n",
    " 560: 'football helmet',\n",
    " 561: 'forklift',\n",
    " 562: 'fountain',\n",
    " 563: 'fountain pen',\n",
    " 564: 'four-poster',\n",
    " 565: 'freight car',\n",
    " 566: 'French horn, horn',\n",
    " 567: 'frying pan, frypan, skillet',\n",
    " 568: 'fur coat',\n",
    " 569: 'garbage truck, dustcart',\n",
    " 570: 'gasmask, respirator, gas helmet',\n",
    " 571: 'gas pump, gasoline pump, petrol pump, island dispenser',\n",
    " 572: 'goblet',\n",
    " 573: 'go-kart',\n",
    " 574: 'golf ball',\n",
    " 575: 'golfcart, golf cart',\n",
    " 576: 'gondola',\n",
    " 577: 'gong, tam-tam',\n",
    " 578: 'gown',\n",
    " 579: 'grand piano, grand',\n",
    " 580: 'greenhouse, nursery, glasshouse',\n",
    " 581: 'grille, radiator grille',\n",
    " 582: 'grocery store, grocery, food market, market',\n",
    " 583: 'guillotine',\n",
    " 584: 'hair slide',\n",
    " 585: 'hair spray',\n",
    " 586: 'half track',\n",
    " 587: 'hammer',\n",
    " 588: 'hamper',\n",
    " 589: 'hand blower, blow dryer, blow drier, hair dryer, hair drier',\n",
    " 590: 'hand-held computer, hand-held microcomputer',\n",
    " 591: 'handkerchief, hankie, hanky, hankey',\n",
    " 592: 'hard disc, hard disk, fixed disk',\n",
    " 593: 'harmonica, mouth organ, harp, mouth harp',\n",
    " 594: 'harp',\n",
    " 595: 'harvester, reaper',\n",
    " 596: 'hatchet',\n",
    " 597: 'holster',\n",
    " 598: 'home theater, home theatre',\n",
    " 599: 'honeycomb',\n",
    " 600: 'hook, claw',\n",
    " 601: 'hoopskirt, crinoline',\n",
    " 602: 'horizontal bar, high bar',\n",
    " 603: 'horse cart, horse-cart',\n",
    " 604: 'hourglass',\n",
    " 605: 'iPod',\n",
    " 606: 'iron, smoothing iron',\n",
    " 607: \"jack-o'-lantern\",\n",
    " 608: 'jean, blue jean, denim',\n",
    " 609: 'jeep, landrover',\n",
    " 610: 'jersey, T-shirt, tee shirt',\n",
    " 611: 'jigsaw puzzle',\n",
    " 612: 'jinrikisha, ricksha, rickshaw',\n",
    " 613: 'joystick',\n",
    " 614: 'kimono',\n",
    " 615: 'knee pad',\n",
    " 616: 'knot',\n",
    " 617: 'lab coat, laboratory coat',\n",
    " 618: 'ladle',\n",
    " 619: 'lampshade, lamp shade',\n",
    " 620: 'laptop, laptop computer',\n",
    " 621: 'lawn mower, mower',\n",
    " 622: 'lens cap, lens cover',\n",
    " 623: 'letter opener, paper knife, paperknife',\n",
    " 624: 'library',\n",
    " 625: 'lifeboat',\n",
    " 626: 'lighter, light, igniter, ignitor',\n",
    " 627: 'limousine, limo',\n",
    " 628: 'liner, ocean liner',\n",
    " 629: 'lipstick, lip rouge',\n",
    " 630: 'Loafer',\n",
    " 631: 'lotion',\n",
    " 632: 'loudspeaker, speaker, speaker unit, loudspeaker system, speaker system',\n",
    " 633: \"loupe, jeweler's loupe\",\n",
    " 634: 'lumbermill, sawmill',\n",
    " 635: 'magnetic compass',\n",
    " 636: 'mailbag, postbag',\n",
    " 637: 'mailbox, letter box',\n",
    " 638: 'maillot',\n",
    " 639: 'maillot, tank suit',\n",
    " 640: 'manhole cover',\n",
    " 641: 'maraca',\n",
    " 642: 'marimba, xylophone',\n",
    " 643: 'mask',\n",
    " 644: 'matchstick',\n",
    " 645: 'maypole',\n",
    " 646: 'maze, labyrinth',\n",
    " 647: 'measuring cup',\n",
    " 648: 'medicine chest, medicine cabinet',\n",
    " 649: 'megalith, megalithic structure',\n",
    " 650: 'microphone, mike',\n",
    " 651: 'microwave, microwave oven',\n",
    " 652: 'military uniform',\n",
    " 653: 'milk can',\n",
    " 654: 'minibus',\n",
    " 655: 'miniskirt, mini',\n",
    " 656: 'minivan',\n",
    " 657: 'missile',\n",
    " 658: 'mitten',\n",
    " 659: 'mixing bowl',\n",
    " 660: 'mobile home, manufactured home',\n",
    " 661: 'Model T',\n",
    " 662: 'modem',\n",
    " 663: 'monastery',\n",
    " 664: 'monitor',\n",
    " 665: 'moped',\n",
    " 666: 'mortar',\n",
    " 667: 'mortarboard',\n",
    " 668: 'mosque',\n",
    " 669: 'mosquito net',\n",
    " 670: 'motor scooter, scooter',\n",
    " 671: 'mountain bike, all-terrain bike, off-roader',\n",
    " 672: 'mountain tent',\n",
    " 673: 'mouse, computer mouse',\n",
    " 674: 'mousetrap',\n",
    " 675: 'moving van',\n",
    " 676: 'muzzle',\n",
    " 677: 'nail',\n",
    " 678: 'neck brace',\n",
    " 679: 'necklace',\n",
    " 680: 'nipple',\n",
    " 681: 'notebook, notebook computer',\n",
    " 682: 'obelisk',\n",
    " 683: 'oboe, hautboy, hautbois',\n",
    " 684: 'ocarina, sweet potato',\n",
    " 685: 'odometer, hodometer, mileometer, milometer',\n",
    " 686: 'oil filter',\n",
    " 687: 'organ, pipe organ',\n",
    " 688: 'oscilloscope, scope, cathode-ray oscilloscope, CRO',\n",
    " 689: 'overskirt',\n",
    " 690: 'oxcart',\n",
    " 691: 'oxygen mask',\n",
    " 692: 'packet',\n",
    " 693: 'paddle, boat paddle',\n",
    " 694: 'paddlewheel, paddle wheel',\n",
    " 695: 'padlock',\n",
    " 696: 'paintbrush',\n",
    " 697: \"pajama, pyjama, pj's, jammies\",\n",
    " 698: 'palace',\n",
    " 699: 'panpipe, pandean pipe, syrinx',\n",
    " 700: 'paper towel',\n",
    " 701: 'parachute, chute',\n",
    " 702: 'parallel bars, bars',\n",
    " 703: 'park bench',\n",
    " 704: 'parking meter',\n",
    " 705: 'passenger car, coach, carriage',\n",
    " 706: 'patio, terrace',\n",
    " 707: 'pay-phone, pay-station',\n",
    " 708: 'pedestal, plinth, footstall',\n",
    " 709: 'pencil box, pencil case',\n",
    " 710: 'pencil sharpener',\n",
    " 711: 'perfume, essence',\n",
    " 712: 'Petri dish',\n",
    " 713: 'photocopier',\n",
    " 714: 'pick, plectrum, plectron',\n",
    " 715: 'pickelhaube',\n",
    " 716: 'picket fence, paling',\n",
    " 717: 'pickup, pickup truck',\n",
    " 718: 'pier',\n",
    " 719: 'piggy bank, penny bank',\n",
    " 720: 'pill bottle',\n",
    " 721: 'pillow',\n",
    " 722: 'ping-pong ball',\n",
    " 723: 'pinwheel',\n",
    " 724: 'pirate, pirate ship',\n",
    " 725: 'pitcher, ewer',\n",
    " 726: \"plane, carpenter's plane, woodworking plane\",\n",
    " 727: 'planetarium',\n",
    " 728: 'plastic bag',\n",
    " 729: 'plate rack',\n",
    " 730: 'plow, plough',\n",
    " 731: \"plunger, plumber's helper\",\n",
    " 732: 'Polaroid camera, Polaroid Land camera',\n",
    " 733: 'pole',\n",
    " 734: 'police van, police wagon, paddy wagon, patrol wagon, wagon, black Maria',\n",
    " 735: 'poncho',\n",
    " 736: 'pool table, billiard table, snooker table',\n",
    " 737: 'pop bottle, soda bottle',\n",
    " 738: 'pot, flowerpot',\n",
    " 739: \"potter's wheel\",\n",
    " 740: 'power drill',\n",
    " 741: 'prayer rug, prayer mat',\n",
    " 742: 'printer',\n",
    " 743: 'prison, prison house',\n",
    " 744: 'projectile, missile',\n",
    " 745: 'projector',\n",
    " 746: 'puck, hockey puck',\n",
    " 747: 'punching bag, punch bag, punching ball, punchball',\n",
    " 748: 'purse',\n",
    " 749: 'quill, quill pen',\n",
    " 750: 'quilt, comforter, comfort, puff',\n",
    " 751: 'racer, race car, racing car',\n",
    " 752: 'racket, racquet',\n",
    " 753: 'radiator',\n",
    " 754: 'radio, wireless',\n",
    " 755: 'radio telescope, radio reflector',\n",
    " 756: 'rain barrel',\n",
    " 757: 'recreational vehicle, RV, R.V.',\n",
    " 758: 'reel',\n",
    " 759: 'reflex camera',\n",
    " 760: 'refrigerator, icebox',\n",
    " 761: 'remote control, remote',\n",
    " 762: 'restaurant, eating house, eating place, eatery',\n",
    " 763: 'revolver, six-gun, six-shooter',\n",
    " 764: 'rifle',\n",
    " 765: 'rocking chair, rocker',\n",
    " 766: 'rotisserie',\n",
    " 767: 'rubber eraser, rubber, pencil eraser',\n",
    " 768: 'rugby ball',\n",
    " 769: 'rule, ruler',\n",
    " 770: 'running shoe',\n",
    " 771: 'safe',\n",
    " 772: 'safety pin',\n",
    " 773: 'saltshaker, salt shaker',\n",
    " 774: 'sandal',\n",
    " 775: 'sarong',\n",
    " 776: 'sax, saxophone',\n",
    " 777: 'scabbard',\n",
    " 778: 'scale, weighing machine',\n",
    " 779: 'school bus',\n",
    " 780: 'schooner',\n",
    " 781: 'scoreboard',\n",
    " 782: 'screen, CRT screen',\n",
    " 783: 'screw',\n",
    " 784: 'screwdriver',\n",
    " 785: 'seat belt, seatbelt',\n",
    " 786: 'sewing machine',\n",
    " 787: 'shield, buckler',\n",
    " 788: 'shoe shop, shoe-shop, shoe store',\n",
    " 789: 'shoji',\n",
    " 790: 'shopping basket',\n",
    " 791: 'shopping cart',\n",
    " 792: 'shovel',\n",
    " 793: 'shower cap',\n",
    " 794: 'shower curtain',\n",
    " 795: 'ski',\n",
    " 796: 'ski mask',\n",
    " 797: 'sleeping bag',\n",
    " 798: 'slide rule, slipstick',\n",
    " 799: 'sliding door',\n",
    " 800: 'slot, one-armed bandit',\n",
    " 801: 'snorkel',\n",
    " 802: 'snowmobile',\n",
    " 803: 'snowplow, snowplough',\n",
    " 804: 'soap dispenser',\n",
    " 805: 'soccer ball',\n",
    " 806: 'sock',\n",
    " 807: 'solar dish, solar collector, solar furnace',\n",
    " 808: 'sombrero',\n",
    " 809: 'soup bowl',\n",
    " 810: 'space bar',\n",
    " 811: 'space heater',\n",
    " 812: 'space shuttle',\n",
    " 813: 'spatula',\n",
    " 814: 'speedboat',\n",
    " 815: \"spider web, spider's web\",\n",
    " 816: 'spindle',\n",
    " 817: 'sports car, sport car',\n",
    " 818: 'spotlight, spot',\n",
    " 819: 'stage',\n",
    " 820: 'steam locomotive',\n",
    " 821: 'steel arch bridge',\n",
    " 822: 'steel drum',\n",
    " 823: 'stethoscope',\n",
    " 824: 'stole',\n",
    " 825: 'stone wall',\n",
    " 826: 'stopwatch, stop watch',\n",
    " 827: 'stove',\n",
    " 828: 'strainer',\n",
    " 829: 'streetcar, tram, tramcar, trolley, trolley car',\n",
    " 830: 'stretcher',\n",
    " 831: 'studio couch, day bed',\n",
    " 832: 'stupa, tope',\n",
    " 833: 'submarine, pigboat, sub, U-boat',\n",
    " 834: 'suit, suit of clothes',\n",
    " 835: 'sundial',\n",
    " 836: 'sunglass',\n",
    " 837: 'sunglasses, dark glasses, shades',\n",
    " 838: 'sunscreen, sunblock, sun blocker',\n",
    " 839: 'suspension bridge',\n",
    " 840: 'swab, swob, mop',\n",
    " 841: 'sweatshirt',\n",
    " 842: 'swimming trunks, bathing trunks',\n",
    " 843: 'swing',\n",
    " 844: 'switch, electric switch, electrical switch',\n",
    " 845: 'syringe',\n",
    " 846: 'table lamp',\n",
    " 847: 'tank, army tank, armored combat vehicle, armoured combat vehicle',\n",
    " 848: 'tape player',\n",
    " 849: 'teapot',\n",
    " 850: 'teddy, teddy bear',\n",
    " 851: 'television, television system',\n",
    " 852: 'tennis ball',\n",
    " 853: 'thatch, thatched roof',\n",
    " 854: 'theater curtain, theatre curtain',\n",
    " 855: 'thimble',\n",
    " 856: 'thresher, thrasher, threshing machine',\n",
    " 857: 'throne',\n",
    " 858: 'tile roof',\n",
    " 859: 'toaster',\n",
    " 860: 'tobacco shop, tobacconist shop, tobacconist',\n",
    " 861: 'toilet seat',\n",
    " 862: 'torch',\n",
    " 863: 'totem pole',\n",
    " 864: 'tow truck, tow car, wrecker',\n",
    " 865: 'toyshop',\n",
    " 866: 'tractor',\n",
    " 867: 'trailer truck, tractor trailer, trucking rig, rig, articulated lorry, semi',\n",
    " 868: 'tray',\n",
    " 869: 'trench coat',\n",
    " 870: 'tricycle, trike, velocipede',\n",
    " 871: 'trimaran',\n",
    " 872: 'tripod',\n",
    " 873: 'triumphal arch',\n",
    " 874: 'trolleybus, trolley coach, trackless trolley',\n",
    " 875: 'trombone',\n",
    " 876: 'tub, vat',\n",
    " 877: 'turnstile',\n",
    " 878: 'typewriter keyboard',\n",
    " 879: 'umbrella',\n",
    " 880: 'unicycle, monocycle',\n",
    " 881: 'upright, upright piano',\n",
    " 882: 'vacuum, vacuum cleaner',\n",
    " 883: 'vase',\n",
    " 884: 'vault',\n",
    " 885: 'velvet',\n",
    " 886: 'vending machine',\n",
    " 887: 'vestment',\n",
    " 888: 'viaduct',\n",
    " 889: 'violin, fiddle',\n",
    " 890: 'volleyball',\n",
    " 891: 'waffle iron',\n",
    " 892: 'wall clock',\n",
    " 893: 'wallet, billfold, notecase, pocketbook',\n",
    " 894: 'wardrobe, closet, press',\n",
    " 895: 'warplane, military plane',\n",
    " 896: 'washbasin, handbasin, washbowl, lavabo, wash-hand basin',\n",
    " 897: 'washer, automatic washer, washing machine',\n",
    " 898: 'water bottle',\n",
    " 899: 'water jug',\n",
    " 900: 'water tower',\n",
    " 901: 'whiskey jug',\n",
    " 902: 'whistle',\n",
    " 903: 'wig',\n",
    " 904: 'window screen',\n",
    " 905: 'window shade',\n",
    " 906: 'Windsor tie',\n",
    " 907: 'wine bottle',\n",
    " 908: 'wing',\n",
    " 909: 'wok',\n",
    " 910: 'wooden spoon',\n",
    " 911: 'wool, woolen, woollen',\n",
    " 912: 'worm fence, snake fence, snake-rail fence, Virginia fence',\n",
    " 913: 'wreck',\n",
    " 914: 'yawl',\n",
    " 915: 'yurt',\n",
    " 916: 'web site, website, internet site, site',\n",
    " 917: 'comic book',\n",
    " 918: 'crossword puzzle, crossword',\n",
    " 919: 'street sign',\n",
    " 920: 'traffic light, traffic signal, stoplight',\n",
    " 921: 'book jacket, dust cover, dust jacket, dust wrapper',\n",
    " 922: 'menu',\n",
    " 923: 'plate',\n",
    " 924: 'guacamole',\n",
    " 925: 'consomme',\n",
    " 926: 'hot pot, hotpot',\n",
    " 927: 'trifle',\n",
    " 928: 'ice cream, icecream',\n",
    " 929: 'ice lolly, lolly, lollipop, popsicle',\n",
    " 930: 'French loaf',\n",
    " 931: 'bagel, beigel',\n",
    " 932: 'pretzel',\n",
    " 933: 'cheeseburger',\n",
    " 934: 'hotdog, hot dog, red hot',\n",
    " 935: 'mashed potato',\n",
    " 936: 'head cabbage',\n",
    " 937: 'broccoli',\n",
    " 938: 'cauliflower',\n",
    " 939: 'zucchini, courgette',\n",
    " 940: 'spaghetti squash',\n",
    " 941: 'acorn squash',\n",
    " 942: 'butternut squash',\n",
    " 943: 'cucumber, cuke',\n",
    " 944: 'artichoke, globe artichoke',\n",
    " 945: 'bell pepper',\n",
    " 946: 'cardoon',\n",
    " 947: 'mushroom',\n",
    " 948: 'Granny Smith',\n",
    " 949: 'strawberry',\n",
    " 950: 'orange',\n",
    " 951: 'lemon',\n",
    " 952: 'fig',\n",
    " 953: 'pineapple, ananas',\n",
    " 954: 'banana',\n",
    " 955: 'jackfruit, jak, jack',\n",
    " 956: 'custard apple',\n",
    " 957: 'pomegranate',\n",
    " 958: 'hay',\n",
    " 959: 'carbonara',\n",
    " 960: 'chocolate sauce, chocolate syrup',\n",
    " 961: 'dough',\n",
    " 962: 'meat loaf, meatloaf',\n",
    " 963: 'pizza, pizza pie',\n",
    " 964: 'potpie',\n",
    " 965: 'burrito',\n",
    " 966: 'red wine',\n",
    " 967: 'espresso',\n",
    " 968: 'cup',\n",
    " 969: 'eggnog',\n",
    " 970: 'alp',\n",
    " 971: 'bubble',\n",
    " 972: 'cliff, drop, drop-off',\n",
    " 973: 'coral reef',\n",
    " 974: 'geyser',\n",
    " 975: 'lakeside, lakeshore',\n",
    " 976: 'promontory, headland, head, foreland',\n",
    " 977: 'sandbar, sand bar',\n",
    " 978: 'seashore, coast, seacoast, sea-coast',\n",
    " 979: 'valley, vale',\n",
    " 980: 'volcano',\n",
    " 981: 'ballplayer, baseball player',\n",
    " 982: 'groom, bridegroom',\n",
    " 983: 'scuba diver',\n",
    " 984: 'rapeseed',\n",
    " 985: 'daisy',\n",
    " 986: \"yellow lady's slipper, yellow lady-slipper, Cypripedium calceolus, Cypripedium parviflorum\",\n",
    " 987: 'corn',\n",
    " 988: 'acorn',\n",
    " 989: 'hip, rose hip, rosehip',\n",
    " 990: 'buckeye, horse chestnut, conker',\n",
    " 991: 'coral fungus',\n",
    " 992: 'agaric',\n",
    " 993: 'gyromitra',\n",
    " 994: 'stinkhorn, carrion fungus',\n",
    " 995: 'earthstar',\n",
    " 996: 'hen-of-the-woods, hen of the woods, Polyporus frondosus, Grifola frondosa',\n",
    " 997: 'bolete',\n",
    " 998: 'ear, spike, capitulum',\n",
    " 999: 'toilet tissue, toilet paper, bathroom tissue'}"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "08576152",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-27T10:14:18.561884Z",
     "start_time": "2022-12-27T10:14:18.537848Z"
    },
    "code_folding": [
     0,
     11,
     24
    ]
   },
   "source": [
    "class GaussianNoiseAdder(object):\n",
    "    def __init__(self, mean=0., std=1.):\n",
    "        self.std = std\n",
    "        self.mean = mean\n",
    "        \n",
    "    def __call__(self, tensor):\n",
    "        return tensor + torch.randn(tensor.size()) * self.std + self.mean\n",
    "    \n",
    "    def __repr__(self):\n",
    "        return self.__class__.__name__ + '(mean={0}, std={1})'.format(self.mean, self.std)\n",
    "\n",
    "def preprocess_image(image: torch.Tensor, custom: bool) -> torch.Tensor:\n",
    "    image = torch.clamp(image, 0, 255).to(torch.uint8)  # remove any values outside [0,255]\n",
    "    if custom:\n",
    "        image = torchvision.transforms.functional.resize(image, [224, 224])\n",
    "        image = image.float() / 255.  # min-max norm to [0-1]\n",
    "        imagenet_normalize = torchvision.transforms.Normalize(\n",
    "            mean=[0.485, 0.456, 0.406],\n",
    "            std=[0.229, 0.224, 0.225])\n",
    "        image = imagenet_normalize(image)\n",
    "    else:  # equivalent to above\n",
    "        image = preprocessor(image)\n",
    "    return image.unsqueeze(0)\n",
    "\n",
    "def generate_model_output(model: torchvision.models, image: torch.Tensor, custom_preproc: bool = True):\n",
    "    # preprocess image\n",
    "    image = preprocess_image(image, custom_preproc)\n",
    "\n",
    "    output = model(image).squeeze(0).softmax(0)\n",
    "    class_id = output.argmax().item()\n",
    "    class_conf = output[class_id].item()\n",
    "    cat_name = weights.meta[\"categories\"][class_id]\n",
    "\n",
    "    return class_id, class_conf, cat_name"
   ],
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "bb2f1c94",
   "metadata": {},
   "source": [
    "### Load  preprocessor, model, and loss function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "fd4825ed",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-27T10:14:19.422693Z",
     "start_time": "2022-12-27T10:14:19.145477Z"
    }
   },
   "source": [
    "weights = ResNet50_Weights.IMAGENET1K_V2\n",
    "preprocessor = weights.transforms()\n",
    "\n",
    "loss_fn = nn.CrossEntropyLoss()\n",
    "model = resnet50(weights=weights)\n",
    "model.eval()"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4a984a55",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-27T10:14:19.509069Z",
     "start_time": "2022-12-27T10:14:19.464715Z"
    }
   },
   "source": [
    "image_path = \"images/dog.jpeg\"\n",
    "class_id_orig, class_conf_orig, cat_name_orig = generate_model_output(model, read_image(image_path))\n",
    "class_id_orig, class_conf_orig, cat_name_orig"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "48c4efe8",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-27T10:14:19.825200Z",
     "start_time": "2022-12-27T10:14:19.782602Z"
    }
   },
   "source": [
    "image_path = \"images/dog.jpeg\"\n",
    "nmean, nstd = 0., 100.\n",
    "noise = GaussianNoiseAdder(mean=nmean, std=nstd)\n",
    "class_id_noise, class_conf_noise, cat_name_noise = generate_model_output(model, noise(read_image(image_path)))\n",
    "class_id_noise, class_conf_noise, cat_name_noise "
   ],
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "094a49cf",
   "metadata": {},
   "source": [
    "## Fast Gradient Sign Method (FGSM) Attack"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c35c5475",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-27T10:14:22.614881Z",
     "start_time": "2022-12-27T10:14:22.451360Z"
    }
   },
   "source": [
    "# generate adversarial image\n",
    "target_label = class_id_orig\n",
    "epsilon = 0.94\n",
    "image = read_image(image_path)\n",
    "image = preprocess_image(image, custom=True)\n",
    "\n",
    "image.requires_grad = True\n",
    "pred = model(image)\n",
    "\n",
    "# single step least likely method. Make the target label the least likely class\n",
    "# ll_class_id = pred.squeeze(0).softmax(0).argmin().item()\n",
    "# target_label = ll_class_id\n",
    "\n",
    "# calculate the gradient of the loss with respect to the image\n",
    "target = torch.Tensor([target_label]).long()\n",
    "loss = loss_fn(pred, target)\n",
    "model.zero_grad()\n",
    "loss.backward()\n",
    "\n",
    "# create the adversarial image\n",
    "grad = image.grad.data\n",
    "grad_sign = grad.sign()\n",
    "perturbed_image = image + epsilon*grad_sign\n",
    "# de-norm perturbed image\n",
    "inv_imagenet_normalize = torchvision.transforms.Normalize(\n",
    "    mean=[-0.485/0.229, -0.456/0.224, -0.406/0.255],\n",
    "    std=[1/0.229, 1/0.224, 1/0.255]\n",
    ")\n",
    "perturbed_image = inv_imagenet_normalize(perturbed_image)\n",
    "perturbed_image = torch.clamp(perturbed_image, 0, 1)"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "9b2fb67b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-27T10:14:22.908533Z",
     "start_time": "2022-12-27T10:14:22.869022Z"
    }
   },
   "source": [
    "# check the model's prediction for the perturbed sample\n",
    "generate_model_output(model, perturbed_image.squeeze()*255.)"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "7c652dd7",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-27T10:14:23.344856Z",
     "start_time": "2022-12-27T10:14:23.336130Z"
    }
   },
   "source": [
    "# save the perturbed adv image\n",
    "adv_image_path = \"images/adv_fgsm.png\"\n",
    "\n",
    "perturbed_image_cv2 = perturbed_image.squeeze().permute(1, 2, 0).detach().cpu().numpy()\n",
    "perturbed_image_cv2 = (perturbed_image_cv2 * 255.).astype(\"uint8\")\n",
    "cv2.imwrite(adv_image_path, perturbed_image_cv2[:, :, ::-1])"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "5aea2013",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-27T10:14:25.514508Z",
     "start_time": "2022-12-27T10:14:25.472388Z"
    }
   },
   "source": [
    "# predict on the saved perturbed image, image compression when writing might cause change in pred\n",
    "class_id_pert, class_conf_pert, cat_name_pert = generate_model_output(model, read_image(adv_image_path))\n",
    "class_id_pert, class_conf_pert, cat_name_pert"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "c72ffa3f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-27T10:14:26.119757Z",
     "start_time": "2022-12-27T10:14:25.916245Z"
    }
   },
   "source": [
    "f, axs = plt.subplots(1, 3, figsize=(12,8))\n",
    "\n",
    "orig_image_cv2 = cv2.resize(cv2.imread(image_path)[:, :, ::-1], (224, 244))\n",
    "\n",
    "noise = GaussianNoiseAdder(mean=nmean, std=nstd)\n",
    "noisy_orig_image = torch.clamp(noise(read_image(image_path)), 0, 255).numpy().transpose(1,2,0).astype(np.uint8)\n",
    "\n",
    "axs[0].imshow(orig_image_cv2)\n",
    "axs[0].set_title(\"Original\")\n",
    "axs[0].set_xlabel(f\"{cat_name_orig}: {class_conf_orig:.2f}\")\n",
    "\n",
    "axs[1].imshow(perturbed_image_cv2)\n",
    "axs[1].set_title(f\"Perturbed (epsilon:{epsilon:.4f})\")\n",
    "axs[1].set_xlabel(f\"{cat_name_pert}: {class_conf_pert:.2f}\")\n",
    "\n",
    "axs[2].imshow(noisy_orig_image)\n",
    "axs[2].set_title(f\"Gaussian Noise (mean={nmean}, sigma={nstd})\")\n",
    "axs[2].set_xlabel(f\"{cat_name_noise}: {class_conf_noise:.2f}\")\n",
    "plt.show()"
   ],
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "e41aa7f7",
   "metadata": {},
   "source": [
    "## R-FGSM Attack"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "c41e5525",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-27T10:30:18.955052Z",
     "start_time": "2022-12-27T10:30:18.847094Z"
    }
   },
   "source": [
    "# generate adversarial image\n",
    "epsilon = 0.3\n",
    "alpha = 0.09 # must be smaller than epsilon\n",
    "target_label = class_id_orig\n",
    "image = read_image(image_path)\n",
    "image = preprocess_image(image, custom=True)\n",
    "\n",
    "image.requires_grad = True\n",
    "target = torch.Tensor([target_label]).long()\n",
    "\n",
    "normal = torch.normal(0., 1., image.shape)\n",
    "image_prime = image + alpha * normal.sign()\n",
    "\n",
    "image_prime.retain_grad()\n",
    "# calculate the gradient of the loss with respect to the image\n",
    "loss = loss_fn(model(image_prime), target)\n",
    "model.zero_grad()\n",
    "loss.backward()\n",
    "\n",
    "# create the adversarial image\n",
    "grad = image_prime.grad.data\n",
    "grad_sign = grad.sign()\n",
    "perturbed_image = image_prime + (epsilon - alpha)*grad_sign\n",
    "\n",
    "# de-norm perturbed image\n",
    "inv_imagenet_normalize = torchvision.transforms.Normalize(\n",
    "    mean=[-0.485/0.229, -0.456/0.224, -0.406/0.255],\n",
    "    std=[1/0.229, 1/0.224, 1/0.255]\n",
    ")\n",
    "perturbed_image = inv_imagenet_normalize(perturbed_image)\n",
    "perturbed_image = torch.clamp(perturbed_image, 0, 1)"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "392c7477",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-27T10:30:19.042831Z",
     "start_time": "2022-12-27T10:30:19.008821Z"
    }
   },
   "source": [
    "# check the model's prediction for the perturbed sample\n",
    "generate_model_output(model, perturbed_image.squeeze()*255.)"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "27aac93f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-27T10:30:21.828617Z",
     "start_time": "2022-12-27T10:30:21.820805Z"
    }
   },
   "source": [
    "# save the perturbed adv image\n",
    "adv_image_path = \"images/adv_rfgsm.png\"\n",
    "\n",
    "perturbed_image_cv2 = perturbed_image.squeeze().permute(1, 2, 0).detach().cpu().numpy()\n",
    "perturbed_image_cv2 = (perturbed_image_cv2 * 255.).astype(\"uint8\")\n",
    "cv2.imwrite(adv_image_path, perturbed_image_cv2[:, :, ::-1])"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "76330135",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-27T10:30:22.249696Z",
     "start_time": "2022-12-27T10:30:22.210437Z"
    }
   },
   "source": [
    "# predict on the saved perturbed image, image compression when writing might cause change in pred\n",
    "class_id_pert, class_conf_pert, cat_name_pert = generate_model_output(model, read_image(adv_image_path))\n",
    "class_id_pert, class_conf_pert, cat_name_pert"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "ebc90763",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-27T10:30:22.890252Z",
     "start_time": "2022-12-27T10:30:22.617961Z"
    }
   },
   "source": [
    "f, axs = plt.subplots(1, 3, figsize=(12,8))\n",
    "\n",
    "orig_image_cv2 = cv2.resize(cv2.imread(image_path)[:, :, ::-1], (224, 244))\n",
    "\n",
    "noise = GaussianNoiseAdder(mean=nmean, std=nstd)\n",
    "noisy_orig_image = torch.clamp(noise(read_image(image_path)), 0, 255).numpy().transpose(1,2,0).astype(np.uint8)\n",
    "\n",
    "axs[0].imshow(orig_image_cv2)\n",
    "axs[0].set_title(\"Original\")\n",
    "axs[0].set_xlabel(f\"{cat_name_orig}: {class_conf_orig:.2f}\")\n",
    "\n",
    "axs[1].imshow(perturbed_image_cv2)\n",
    "axs[1].set_title(f\"Perturbed (epsilon:{epsilon:.4f})\")\n",
    "axs[1].set_xlabel(f\"{cat_name_pert}: {class_conf_pert:.2f}\")\n",
    "\n",
    "axs[2].imshow(noisy_orig_image)\n",
    "axs[2].set_title(f\"Gaussian Noise (mean={nmean}, sigma={nstd})\")\n",
    "axs[2].set_xlabel(f\"{cat_name_noise}: {class_conf_noise:.2f}\")\n",
    "plt.show()"
   ],
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "e7ceff2d",
   "metadata": {},
   "source": [
    "## Torchattack"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "5d78daf7",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-26T16:22:14.283113Z",
     "start_time": "2022-12-26T16:22:14.024639Z"
    }
   },
   "source": [
    "import torchattacks\n",
    "\n",
    "weights = ResNet50_Weights.IMAGENET1K_V2\n",
    "model = resnet50(weights=weights)\n",
    "\n",
    "# atk1 = torchattacks.FGSM(model, eps=10/255)\n",
    "atk1 = torchattacks.PGD(model, eps=8/255, alpha=2/225, steps=10, random_start=True)\n",
    "atk1.set_normalization_used(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])\n",
    "\n",
    "image_path = \"images/dog.jpeg\""
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "d1378d73",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-26T16:22:14.334466Z",
     "start_time": "2022-12-26T16:22:14.284985Z"
    }
   },
   "source": [
    "class_id_orig, class_conf_orig, cat_name_orig = generate_model_output(model, read_image(image_path))\n",
    "target = torch.Tensor([class_id_orig]).long()\n",
    "class_id_orig, class_conf_orig, cat_name_orig"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "4d1414c5",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-26T16:22:14.525496Z",
     "start_time": "2022-12-26T16:22:14.520166Z"
    }
   },
   "source": [
    "image = preprocess_image(read_image(image_path), custom=True)"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "b8f1c4f9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-26T16:22:15.567639Z",
     "start_time": "2022-12-26T16:22:14.710218Z"
    }
   },
   "source": [
    "adv_image = atk1(image, target).squeeze()\n",
    "adv_image = atk1.inverse_normalize(adv_image)  # inv imagenet normalize"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "63acb53f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-26T16:22:15.614510Z",
     "start_time": "2022-12-26T16:22:15.568941Z"
    }
   },
   "source": [
    "# adversarial image prediction\n",
    "generate_model_output(model, adv_image.squeeze()*255.)"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "49d6491a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-26T16:22:15.618796Z",
     "start_time": "2022-12-26T16:22:15.616175Z"
    }
   },
   "source": [
    "adv_image_cv2 = adv_image.squeeze().permute(1, 2, 0).detach().cpu().numpy()"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "e56260d4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-26T16:22:15.772677Z",
     "start_time": "2022-12-26T16:22:15.654607Z"
    }
   },
   "source": [
    "plt.figure(figsize=(10,10))\n",
    "plt.imshow(adv_image_cv2)\n",
    "plt.show()"
   ],
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8361345c",
   "metadata": {},
   "source": [],
   "outputs": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
